package com.tukez.tmdb
import org.specs2.mutable.Specification
import com.tukez.tmdb.query.RatingBetween
import com.tukez.tmdb.query.Order
import com.tukez.tmdb.query.OrderBy
import com.tukez.tmdb.model.Images
import com.tukez.tmdb.util.TMDbUtil
import com.tukez.tmdb.query.Hash
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner

/** System level tests for TMDb.
 *  Set your API key as system property "tmdb.apikey".
 *  For example: -Dtmdb.apikey=<APIKEY>
 */
@RunWith(classOf[JUnitRunner])
class TMDbIT extends Specification {

  private val apikey = System.getProperty("tmdb.apikey")
  if (apikey eq null) throw new Exception("""API key not found. Set your API key as system property "tmdb.apikey"""")

  private val tmdb = new TMDb(apikey)

  "TMDb" should {

    // Disabled because Media.getInfo does not currently work in the API
    //"do successful Media.getInfo" in {
    //  val movies = tmdb.Media.getInfo(Hash("907172e7fe51ba57", 742086656))
    //  movies must not beEmpty
    //}

    "do successful Movie.browse" in {
      val movies = tmdb.Movie.browse(OrderBy.Rating, Order.Asc, query = "Blade Runner", rating = RatingBetween(7, 10))
      movies must not beEmpty
      val bladeRunner = movies find { _.name == "Blade Runner" }
      bladeRunner must beSome
    }

    "do successful Movie.getImages" in {
      val Images(posters, backdrops) = tmdb.Movie.getImages("tt0083658")
      posters must not beEmpty;
      backdrops must not beEmpty
    }

    "do successful Movie.getInfo" in {
      val movie = tmdb.Movie.getInfo(78)
      movie must beSome
      movie.get.id must_== 78
      movie.get.name must_== "Blade Runner"
    }

    "do successful Movie.getLatest" in {
      val movie = tmdb.Movie.getLatest
      movie.name must not beEmpty
    }

    "do successful Movie.getTranslations" in {
      val translations = tmdb.Movie.getTranslations(78)
      translations must not beEmpty
    }

    "do successful Movie.getVersion" in {
      val versions = tmdb.Movie.getVersion(78, 65, 27205)
      versions must have size 3
    }

    "do successful Movie.imdbLookup" in {
      val movie = tmdb.Movie.imdbLookup("tt0083658")
      movie must beSome
      movie.get.id must_== 78
      movie.get.name must_== "Blade Runner"
    }

    "do successful Movie.search" in {
      val movies = tmdb.Movie.search("Blade")
      movies must not beEmpty
      val bladeRunner = movies find { _.name == "Blade Runner" }
      bladeRunner must beSome
    }

    "do successful Person.getInfo" in {
      val person = tmdb.Person.getInfo(3)
      person must beSome
      person.get.id must_== 3
      person.get.name must_== "Harrison Ford"
    }

    "do successful Person.getLatest" in {
      val person = tmdb.Person.getLatest
      person.name must not beEmpty
    }

    "do successful Person.search" in {
      val persons = tmdb.Person.search("Harrison Ford")
      persons must not beEmpty
      val harrisonFord = persons find { _.name == "Harrison Ford" }
      harrisonFord must beSome
    }

    "do successful Genres.getList" in {
      val genres = tmdb.Genres.getList
      genres must not beEmpty
    }

    "throw TMDbHttpException with error details" in {
      val invalidTmdb = new TMDb("APIKEY")
      invalidTmdb.Genres.getList must throwA[TMDbHttpException].like {
        case e: TMDbHttpException => {
          e.error must beSome
          e.error.get.status_code must_== 7
        }
      }
    }

  }

}